<?php

// ** Documentation
/*______________________________________________________________________________

	Greqo Blogger API Test

	Last Update: Dec 2006
	Author: Tom at klenwell@gmail.com
	Website: http://code.google.com/p/greqo/

	SUMMARY
		Post a entry to a Blogger blog using new API 
	
	NOTES
		one call to greqo_driver.inc.php loads Greqo library
		put login settings in blogger_data.inc.php file
		
		This should be compatible with later versions of PHP 4.
	
	REFERENCES
		http://code.google.com/apis/accounts/AuthForInstalledApps.html#Request
		http://code.google.com/apis/gdata/index.html
		http://pear.php.net/package/HTTP/redirected
		http://magpierss.sourceforge.net/
		http://www.php.net/manual/en/function.fsockopen.php#67395
		
______________________________________________________________________________*/
 
// *** Initialize Session
error_reporting(E_ALL);
session_start();
 
// *** Load Greqo Driver
$_DIR['greqo'] = '../';
require_once($_DIR['greqo'] . 'greqo_driver.inc.php');
 
// *** Load Blogger User Login File
require_once('../blogger_data.inc.php');

// *** Script Variables

	// Event Triggers
	$_TRIGGER['get_token'] = 0;
	$_TRIGGER['test_post'] = 0;
	
	// SESSION Token
	$_SESSION['GDATA']['auth_token'] = ( isset($_SESSION['GDATA']['auth_token']) ) ? $_SESSION['GDATA']['auth_token']: 0;
	
	// Test Post Data (brought to you by Greqo -- you can edit title and body)
  $greqo_url = 'http://code.google.com/p/greqo/';
  $test_post_title = 'greqo test post';
  $test_post_body = '<div class="greqo_test">this is a test of the greqo open source blogger api<div style="margin:4px 0; color:#660000; font-size:11px; font-family:Verdana, Geneva, sans-serif;">information available at <a href="' . $greqo_url . '">greqo project page</a></div></div>';
  // note: linebreaks removed above to control for blogger auto-line-breaking
	
	// HTML
	$_HTML['body'] = '';
	$_HTML['test_bx'] = '<input type="submit" name="test_post" value="post test entry now!" />';
	$_HTML['form'] = '';
	$_HTML['token'] = "<div style='font-size:8px;'>current blogger token: {$_SESSION['GDATA']['auth_token']}</div>";
	$_HTML['result'] = '<h2>click button to post a test entry</h2>make sure script is properly configured with correct settings';

	
// *** Controller
if ( !empty($_POST['test_post']) )
{
	$_TRIGGER['get_token'] = 1;		
}


// *** EVENTS

// * Get Token
if ( $_TRIGGER['get_token'] )
{

	$_HTML['test_bx'] = '';
	$_HTML['result'] .= '<p style="font-size:10px; color:#336699;">attempting to retrieve gdata auth token</p>';

	// Request token using global settings 
	if ( greqo_request_clientlogin_token($GREQO['BLOGGER']['user'], $GREQO['BLOGGER']['password'], 'blogger', 1) ) 
	{
		$_HTML['result'] .= <<<HTML
<h4 style="color:green;">token for user {$_BLOGGER['user']} retrieved</h4>
<div style="margin:1em; font-size:9px; color:#999;">token: {$_SESSION['GDATA']['auth_token']}<b></div>
HTML;
		
		$_TRIGGER['test_post'] = 1;
	}
	else
	{
		trigger_error('unable to retrieve token', E_USER_WARNING);
	}
}

// * Test Post
if ( $_TRIGGER['test_post'] )
{
	$_HTML['result'] .= '<p style="font-size:10px; color:#336699;">attempting to post entry using Greqo library</p>';
	
	// Post
	if ( $POST_DATA = greqo_blogger_post_entry($test_post_body, $test_post_title) )
	{
		$_HTML['result'] .= '<p style="color:green;">post succeeded</p>';
		$_response = print_r($POST_DATA,1);
		$_HTML['result'] .= "<pre>{$_response}</pre>";
		$_HTML['result'] .= "<p><a href=\"{$POST_DATA['self_link']}\">click to see post</a></p>";
	}
	else
	{
		$_HTML['result'] .= '<p style="color:red;">post failed</p>';
	}
}


// *** Output

	// form
	$show_body = htmlspecialchars($test_post_body);
	$_HTML['form'] = <<<FORM
<h4></h4>
<p>will post following message:</p>
<pre style="width:60%; margin:10px; padding:10px 4px; border:1px solid #ccf; overflow-x:auto;">
<b>$test_post_title</b><br />
$show_body
</pre>
<form name="greqo_auth" method="post" action="{$_SERVER['PHP_SELF']}">
{$_HTML['test_bx']}
</form>
FORM;
		
		$_HTML['body'] = <<<HTML
<h1>Greqo Post Test</h1>
{$_HTML['result']}
{$_HTML['form']}
<h6><a href="http://code.google.com/p/greqo/">greqo project page</a></h6>

HTML;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Greqo Post Test</title>
<style type="text/css">body { font-family:arial,sans-serif; color:#999; }</style>
</head>
<body>
<?php echo $_HTML['body'] ?>
</body>
</html>

